<?php

namespace admin\models;

use Yii;

class UserWithdraw extends \common\models\UserWithdraw
{
    public $start_time;
    public $end_time;

    public function rules()
    {
        return array_merge(parent::rules(), [
            // [['field1', 'field2'], 'required', 'message' => '{attribute} is required'],
        ]);
    }

    public function scenarios()
    {
        return array_merge(parent::scenarios(), [
            // 'scenario' => ['field1', 'field2'],
        ]);
    }

    public function attributeLabels()
    {
        return array_merge(parent::attributeLabels(), [
            'op_state' => '申请状态',
            // 'field2' => 'description2',
        ]);
    }

   public function newoutUserMoney()
    {
        $post = [];
        $reqTime = date('YmdHis',time());
        $post['appId'] = HAPPY_APPID;
        $post['appSecret'] = HAPPY_MDKEY;
        $post['bizCode'] = 'D10002';
        $post['userCode'] = HAPPY_APPID;  // 商户号，与以上APPID一致
        $post['reqTs'] = $reqTime;
        $post['reqSn'] = HAPPY_APPID . $reqTime;
        $post['version'] = '1.1.0';
        if (empty($this->out_sn)) {
            return []; 
        }

        $package['orderCode'] = $this->out_sn;
        $package['payType'] = '07';
        $package['serviceType'] = 'D0';//微信
        $package['orderAmount'] = $this->amount;
        $package['settleAmt'] = strval($this->amount - 3);
        $package['account'] = '13327888823';
        $package['payeeRealName'] = $this->user->userAccount->realname;
        $package['payeeGenBankName'] = $this->user->userAccount->bank_name;
        $package['payeePersonMobile'] = $this->user->userAccount->bank_mobile;
        $package['payeeBankCardNum'] = $this->user->userAccount->bank_card;
        $package['bankCode'] = $this->user->userAccount->bank_code;
        $package['payeeBankProvince'] = $this->user->userAccount->province; // 省份
        $package['payeeBankCity'] = $this->user->userAccount->city; // 城市
        $package['payChannel'] = 'lefu';
        $package['notifyUrl'] = url(['site/admin-klnotify'], true);
        $data = array_merge($post, $package);
        ksort($data);
        $str = '';
        foreach ($data as $key => $val) {
            $str .= "{$key}={$val}&";
        }
        $sign_data = substr($str, 0, -1); //待签名数据
        $sign_data_base64 = base64_encode($sign_data); 
        //获取签名
        $pk = openssl_pkey_get_private(MERCHANT_PRIVATE_KEY);
        $signature = '';
        openssl_sign($sign_data_base64, $signature, $pk);
        $post['content'] = array($package);
        $post['signature'] = base64_encode($signature);

        $request = httpGet(HAPPY_URL, json_encode($post));
        $res = json_decode($request, true);
        test(json_encode($post), $post, $res);
        if($res['content'][0]['code'] == "0000") {
            return  $res['content'][0]['html'];
        }
        return false;



        // $package['out_trade_no'] = $this->out_sn;
        // $package['amount'] = $this->amount * 100;
        // $package['account_no'] = $this->user->userAccount->bank_card; //银行卡或存折号码
        // $package['account_name'] = $this->user->userAccount->realname; //银行卡或存在上的所有人姓名
        // $package['account_type'] = '00'; //账号类型
        // $package['client_ip'] = Yii::$app->request->userIP;;//订单备注
        // $package['id_type'] = '0';
        // $package['id'] = $this->user->userAccount->id_card;
        // $package['bank_code'] = $this->user->userAccount->bank_code; //银行编码
        // ksort($package, SORT_STRING);
        // $string1 = '';
        // foreach($package as $key => $v) {
        //     $string1 .= "{$key}={$v}&";
        // }
        // $string1 = trim($string1, '&') . BXCHANGE_MDKEY;
        // $package['sign'] = md5($string1);
        // $package['sign_type'] = 'MD5';
        // $url = 'http://scpay.shopping98.com/v1/gateway.shtml';
        // $request = httpRequest($url, $package);
        // return json_decode($request, true); 

    }

    public function listQuery()
    {
        return $this->search()
            ->manager()
            ->andFilterWhere(['>=', 'userWithdraw.updated_at', $this->start_time])
            ->andFilterWhere(['<=', 'userWithdraw.updated_at', $this->end_time]);
    }
}
